/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.sun.mail.smtp.SMTPMessage;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.CharsetUtil;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Flag.FlagInfo;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.text.DateFormat;
/*     */ import java.util.Calendar;
/*     */ import java.util.Date;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.TimeZone;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.Transport;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SendDeliveryReport
/*     */   extends MailDocumentHandler
/*     */ {
/*  60 */   private static final String[] TARGET_ITEM_PATH = { "mid" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  64 */     return TARGET_ITEM_PATH;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  69 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  70 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  71 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*  73 */     int msgid = new ItemId(request.getAttribute("mid"), zsc).getId();
/*  74 */     Message msg = mbox.getMessageById(octxt, msgid);
/*     */     
/*     */ 
/*  77 */     if ((mbox.getEffectivePermissions(octxt, msgid, MailItem.Type.MESSAGE) & 0x2) == 0) {
/*  78 */       throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the message");
/*     */     }
/*     */     
/*  81 */     sendReport(getSenderAccount(zsc), msg, false, zsc.getRequestIP(), zsc.getUserAgent());
/*     */     
/*     */ 
/*  84 */     mbox.alterTag(octxt, msgid, MailItem.Type.MESSAGE, Flag.FlagInfo.NOTIFIED, true, null);
/*     */     
/*  86 */     Element response = zsc.createElement(MailConstants.SEND_REPORT_RESPONSE);
/*  87 */     return response;
/*     */   }
/*     */   
/*     */   protected Account getSenderAccount(ZimbraSoapContext zsc) throws ServiceException {
/*  91 */     return getAuthenticatedAccount(zsc);
/*     */   }
/*     */   
/*     */   public static void sendReport(Account authAccount, Message msg, boolean automatic, String requestHost, String userAgent) throws ServiceException
/*     */   {
/*  96 */     MimeMessage mm = msg.getMimeMessage();
/*  97 */     Account owner = msg.getMailbox().getAccount();
/*     */     
/*  99 */     String charset = authAccount.getPrefMailDefaultCharset();
/* 100 */     if (charset == null) {
/* 101 */       charset = "utf-8";
/*     */     }
/*     */     try
/*     */     {
/* 105 */       InternetAddress[] recipients = Mime.parseAddressHeader(mm, "Disposition-Notification-To");
/* 106 */       if ((recipients == null) || (recipients.length == 0)) {
/* 107 */         return;
/*     */       }
/* 109 */       SMTPMessage report = new SMTPMessage(JMSession.getSmtpSession());
/* 110 */       String subject = "Read-Receipt: " + msg.getSubject();
/* 111 */       report.setSubject(subject, CharsetUtil.checkCharset(subject, charset));
/* 112 */       report.setSentDate(new Date());
/* 113 */       report.setFrom(AccountUtil.getFriendlyEmailAddress(authAccount));
/* 114 */       report.addRecipients(Message.RecipientType.TO, recipients);
/* 115 */       report.setHeader("Auto-Submitted", "auto-replied (zimbra; read-receipt)");
/* 116 */       report.setHeader("Precedence", "bulk");
/*     */       
/* 118 */       if (Provisioning.getInstance().getConfig().isAutoSubmittedNullReturnPath()) {
/* 119 */         report.setEnvelopeFrom("<>");
/*     */       } else {
/* 121 */         report.setEnvelopeFrom(authAccount.getName());
/*     */       }
/*     */       
/* 124 */       MimeMultipart multi = new ZMimeMultipart("report");
/*     */       
/*     */ 
/* 127 */       String text = generateTextPart(owner, mm, authAccount.getLocale());
/* 128 */       MimeBodyPart mpText = new ZMimeBodyPart();
/* 129 */       mpText.setText(text, CharsetUtil.checkCharset(text, charset));
/* 130 */       multi.addBodyPart(mpText);
/*     */       
/*     */ 
/* 133 */       String mdn = generateReport(owner, mm, automatic, requestHost, userAgent);
/* 134 */       MimeBodyPart mpMDN = new ZMimeBodyPart();
/* 135 */       mpMDN.setText(mdn, "utf-8");
/* 136 */       mpMDN.setHeader("Content-Type", "message/disposition-notification; charset=utf-8");
/* 137 */       multi.addBodyPart(mpMDN);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 146 */       report.setContent(multi);
/* 147 */       report.setHeader("Content-Type", multi.getContentType() + "; report-type=disposition-notification");
/* 148 */       report.saveChanges();
/*     */       
/* 150 */       Transport.send(report);
/*     */     } catch (MessagingException me) {
/* 152 */       throw ServiceException.FAILURE("error while sending read receipt", me);
/*     */     }
/*     */   }
/*     */   
/*     */   public static String generateTextPart(Account owner, MimeMessage mm, Locale lc) throws MessagingException {
/* 157 */     String subject = Mime.getSubject(mm);
/*     */     
/* 159 */     String dateStr = "???";
/* 160 */     Calendar cal = DateUtil.parseRFC2822DateAsCalendar(mm.getHeader("Date", null));
/* 161 */     if (cal != null) {
/* 162 */       DateFormat format = DateFormat.getDateTimeInstance(1, 1, lc);
/* 163 */       format.setTimeZone(TimeZone.getTimeZone("GMT" + DateUtil.getTimezoneString(cal)));
/* 164 */       dateStr = format.format(cal.getTime());
/*     */     }
/*     */     
/* 167 */     return L10nUtil.getMessage(L10nUtil.MsgKey.readReceiptNotification, lc, new Object[] { dateStr, owner.getName(), subject });
/*     */   }
/*     */   
/*     */   public static String generateReport(Account owner, MimeMessage mm, boolean automatic, String requestHost, String userAgent) throws MessagingException
/*     */   {
/* 172 */     StringBuilder mdn = new StringBuilder();
/*     */     
/* 174 */     if ((userAgent != null) || (requestHost != null)) {
/* 175 */       mdn.append("Reporting-UA: ");
/* 176 */       if ((requestHost != null) && (!requestHost.trim().equals(""))) {
/* 177 */         mdn.append(requestHost).append(userAgent == null ? "" : "; ");
/*     */       }
/* 179 */       if ((userAgent != null) && (!userAgent.trim().equals(""))) {
/* 180 */         mdn.append(userAgent);
/*     */       }
/* 182 */       mdn.append("\r\n");
/*     */     }
/*     */     
/* 185 */     mdn.append("Original-Recipient: rfc822;").append(owner.getName()).append("\r\n");
/* 186 */     mdn.append("Final-Recipient: rfc822;").append(owner.getName()).append("\r\n");
/*     */     
/* 188 */     String messageID = mm.getMessageID();
/* 189 */     if ((messageID != null) && (!messageID.trim().equals(""))) {
/* 190 */       mdn.append("Original-Message-ID: ").append(messageID.trim()).append("\r\n");
/*     */     }
/*     */     
/* 193 */     mdn.append("Disposition: manual-action/MDN-sent-");
/* 194 */     mdn.append(automatic ? "automatically" : "manually");
/* 195 */     mdn.append("; displayed\r\n");
/*     */     
/* 197 */     return mdn.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/SendDeliveryReport.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */